import os


def read_user(self):
    with open(r'../data/user.csv',
              encoding='utf-8') as file:  # r'd:\a.txt'，声明字符串不需要转义  (使用raw string，也就是在string'前面加r，告诉python不需要转义)
        content = file.readlines()

    list = []
    for line in content:
        if line.startswith('#'):
            continue
        dict = {}
        dict['username'] = line.strip().split(',')[0]
        dict['password'] = line.strip().split(',')[1]
        dict['result'] = line.strip().split(',')[2]
        list.append(dict)

    return list


def read_csv(cls, filepath):
    if os.path.exists(filepath):
        with open(filepath, encoding='utf-8') as file:
            content = file.readlines()
        key_list = []
        data_list = []
        for row in range(len(content)):
            # 跳过以#号开头的行及空行
            if content[row].startswith('#') or content[row] == '\n':
                continue
            elif key_list == []:
                key_list = content[row].strip().split(',')
            else:
                value_list = content[row].strip().split(',')
                info = dict(zip(key_list, value_list))
                data_list.append(info)
        return data_list
    else:
        return False
